package com.gitee.luosl.cola.lang;

import java.util.Objects;
import java.util.function.*;

public final class Tuple1<T1> implements Tuple {

    public final T1 _1;

    Tuple1(T1 left) {
        this._1 = left;
    }

    public T1 get_1() {
        return _1;
    }


    public <R> Tuple1<R> map1(Function<? super T1, ? extends R> mapper){
        return Tuple.of(mapper.apply(_1));
    }

    public <R> Tuple1<R> map(Function<? super T1, Tuple1<R>> mapper){
        return mapper.apply(_1);
    }

    public <R> R apply(Function<? super T1, ? extends R> mapper) {
        return mapper.apply(_1);
    }

    public void accept(Consumer<? super T1> consumer) {
        consumer.accept(_1);
    }

    public boolean test(Predicate<? super T1> predicate){
        return predicate.test(_1);
    }

    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(_1);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        } else if (!(obj instanceof Tuple1)) {
            return false;
        } else {
            final Tuple1<?> that = (Tuple1<?>) obj;
            return Objects.equals(this._1, that._1);
        }
    }

    @Override
    public String toString() {
        return String.format(
                "(%s)",
                Objects.toString(_1)
        );
    }
}
